'use strict';
app.controller('modulesCtrl',['$scope','utilService','modulesService','toaster','$modal', function($scope,utilService,modulesService,toaster,$modal){
    $scope.modulesListVO={};

    //查询模块
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'modules',
            params:$scope.modulesListVO
        }).then(function(result){
        });
    }

    $scope.query();

    //删除模块
    $scope.deleteById = function(id, name){
        var param = {
            id:id,
            name:name
        };

        // query data
        modulesService.getModulesGoodsCount($scope, id).then(function(result){
            switch (result.status){
                case 200:
                    if(result.data > 0){
                        param.state=true;
                        openModal("该模块【" + name + "】正在被其他商品使用,请先清空模块下的商品再删除");
                    }else{
                        param.state=false;
                        openModal("你是否要删除【" + name + "】模块？");
                    }
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }

        });

        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"删除模块",
                controller:"deleteModulesCtrl",
                message:msg,
                data:param
            });
        }

    }



    $scope.findNoPageAll=function(){
        modulesService.findNoPageAll($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.columns = result.data;
                    break;
                case 661:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    $scope.findNoPageAll();

}]);

app.controller('deleteModulesCtrl', ['$scope', '$modalInstance','$state','toaster','modalData','modulesService',
    function($scope, $modalInstance,$state,toaster,modalData,modulesService) {
        $scope.modal = modalData;
        $scope.modules = {
            id: modalData.data.id
        };
        $scope.isDelModal=true;

        //确认删除
        $scope.ok = function(){
            if($scope.modal.data.state){
                $modalInstance.dismiss('cancel');
                return;
            }

            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            modulesService.delModules($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");;
                        $scope.isDelModal=true;
                        $scope.query();
                        break;
                    case 615:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

    }
]);